package com.example.dish.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.dish.service.CategoryService;
import com.example.dish_model.entity.Category;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author sjz
 * @since 2024-06-30
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Resource
    private CategoryService categoryService;


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Category>> getPage(long page, long pageSize) {
        return Result.success(categoryService.page(new Page<>(page, pageSize)));
    }

    /**
     * 根据type,反查菜品
     *
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType() != null,Category::getType, category.getType());
        return Result.success(categoryService.list(wrapper));
    }


    /**
     * 新增菜品分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result<Object> add(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }


    /**
     * 删除菜品分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(String ids) {
        log.info("要删除的分类id:{}", ids);
        categoryService.removeCategory(ids);
        return Result.success("分类删除成功");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> getById(@PathVariable("id") String id) {
        return Result.success(categoryService.getById(id));
    }


    /**
     * 修改菜品分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result<Object> update(@RequestBody Category category) {
        log.info("修改菜品分类:{}", category);
        categoryService.updateById(category);
        return Result.success();
    }


}
